package cu.ds.logicalsync.fytasrealm.model;

import java.util.ArrayList;
import java.util.Random;

public class World {
	
	private String worldName;
	private boolean worldStatus; //true=available
	private PositionWorld worldRoom[][] = new PositionWorld[8][8]; //row,col,isoccupy?,islock?
	private ArrayList<Warrior> worldParticipant;
	private PositionMap myPositionInMap;

	public World(String name) {
		this.worldName = name;
		this.worldStatus = true;
	
		int i,j;
		for (i=0; i<8; i++) {
			for (j=0; j<8; j++) {
				PositionWorld pw = new PositionWorld(i,j);
				pw.setPositionOccupy(false);
				pw.setLock(false);
				this.worldRoom[i][j] = pw;
			}
		}
		
		this.worldParticipant = new ArrayList<Warrior>();
		setMyPositionInMap();
		setWorldRoom();
	}
		
	public String getWorldName() {
		return worldName;
	}
	public void setWorldName(String worldName) {
		this.worldName = worldName;
	}
	
	public boolean getWorldStatus() {
		return worldStatus;
	}
	public void setWorldStatus(boolean worldStatus) {
		this.worldStatus = worldStatus;
	}
	
	public PositionWorld[][] getWorldRoom() {
		return worldRoom;
	}
	
	public void setWorldRoom(int row, int col, PositionWorld w) {
		worldRoom[row][col] = w;
	}
	
	/**
	 * @deprecated
	 */
	public void setWorldRoom(int row,int col,String action) {
		PositionWorld pw = new PositionWorld(row, col);
		pw.setRowPosition(row);
		pw.setColPosition(col);
		if (action == "move_left") {
			if (col != 0) {
				pw.setPositionOccupy(false);
				pw.setRowPosition(row);
				pw.setColPosition(col-1);
				pw.setPositionOccupy(true);
			}
		}
		else if (action == "move_right") {
			if (col != 7) {
				pw.setPositionOccupy(false);
				pw.setRowPosition(row);
				pw.setColPosition(col+1);
				pw.setPositionOccupy(true);
			}
		}
		else if (action == "move_up") {
			if (row != 0) {
				pw.setPositionOccupy(false);
				pw.setRowPosition(row-1);
				pw.setColPosition(col);
				pw.setPositionOccupy(true);
			}
		}
		else if (action == "move_down") {
			if (row != 7) {
				pw.setPositionOccupy(false);
				pw.setRowPosition(row+1);
				pw.setColPosition(col);
				pw.setPositionOccupy(true);
			}
		}
	}
	public void setWorldRoom() {
		randomPosition();
	}
	
	public void randomPosition() {
		boolean randomSuccess = false;
		PositionWorld pw = null;
		while (!randomSuccess) {
			Random generator = new Random();
			int randomRow = generator.nextInt(7);
			int randomCol = generator.nextInt(7);
			pw = new PositionWorld(randomRow, randomCol);
			pw.setWarriorNameInWorld(worldName);
			pw.setPositionOccupy(true);
			this.worldRoom[randomRow][randomCol] = pw;
			randomSuccess = true;
		}
	}
	
	
	public void setRoomPositionInFightMode(int row,int col,String action) {
		PositionWorld pw = new PositionWorld(row, col);
		if (action == "fight") { // fight mode
			pw.setLock(true);
		}
		else if (action == "end") {	//fight is end
			pw.setLock(false);
		}
		this.worldRoom[row][col] = pw;
	}
	
	public ArrayList<Warrior> getWorldParticipant() {
		return worldParticipant;
	}
	public void setWorldParticipant(ArrayList<Warrior> worldParticipant) {
		this.worldParticipant = worldParticipant;
	}	
	
	public PositionMap getMyPositionInMap() {
		return myPositionInMap;
	}

	
	public void setMyPositionInMap() {
		Random generator = new Random();
			int randomRow = generator.nextInt(400);
			int randomCol = generator.nextInt(300);
			PositionMap pm = new PositionMap(randomRow, randomCol);
			pm.setPositionOccupy(true);
			pm.setWorldNameInMap(this.worldName);
			pm.setPositionColor("Others", pm.isOccupy());
			this.myPositionInMap = pm;
	}

	public ArrayList<PositionWorld> findOccupiedPositions() {
		ArrayList<PositionWorld> result = new ArrayList<PositionWorld>();
		PositionWorld pw = null;
		
		int i,j;
		for (i=0; i<8; i++) {
			for (j=0; j<8; j++) {
				pw = this.worldRoom[i][j];
				if(pw.isOccupy()) {
					result.add(pw);
				}
			}
		}
		
		return result;
		
	}
}
